package org.example.config;

import org.example.handler.CustomAccessDenyHandler;
import org.example.handler.CustomLoginFailureHandler;
import org.example.handler.CustomLoginSuccessForwardHandler;
import org.example.handler.CustomerLoginOutHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

@Configuration
@EnableWebSecurity
//启用方法级别的认证 prePostEnabled=true表示可以使用@PreAuthorize和@PostAuthrize
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
//    PasswordEncoder passwordEncoder(){
//        //已过时 === 密码不加密的方式
//        return NoOpPasswordEncoder.getInstance();
//    }
    // 加密操作频繁用到
    BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }
@Resource
private UserDetailsService userDetailsService;

    /**  成功处理的内容 */
    @Resource
    private CustomLoginSuccessForwardHandler customLoginSuccessForwardHandler;

    /** 失败处理的内容 */
    @Resource
    private CustomLoginFailureHandler customLoginFailureHandler;

    @Resource
    private CustomAccessDenyHandler customAccessDenyHandler;

    @Resource
    private CustomerLoginOutHandler customerLoginOutHandler;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        //添加内存级别的用户
//        auth.inMemoryAuthentication()
//                .withUser("jason").password(bCryptPasswordEncoder().encode("123456")).roles("USER","ADMIN");
//        auth.inMemoryAuthentication()
//                .withUser("majian").password(bCryptPasswordEncoder().encode("654321")).roles("ADMIN");

        //添加自定义的用户信息，比如用户信息从数据库中拿到，丢到service处理
        auth.userDetailsService(userDetailsService);
    }

    /**
     * 配置自定义的请求的内容，包括页面，出错等具体信息，甚至还可以绑定对应的自定义的json返回
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginProcessingUrl("/login")
                .loginPage("/login.html")
                //成功后实现自定义的处理
               // .successHandler(customLoginSuccessForwardHandler)
                //跳转页面
               // .successForwardUrl("/index")  //跳转到对应的main
              //  .failureForwardUrl("/failure")
                .failureHandler(customLoginFailureHandler);
             //   .loginPage( "/failure.html");

        http.authorizeRequests()
                .antMatchers("/login.html").permitAll() //不认证页面
                .antMatchers("/failure.html").permitAll()
                .anyRequest().authenticated();//其他所有都认证

        //异常处理
        http.exceptionHandling()
                .accessDeniedHandler(customAccessDenyHandler);


        http.logout().addLogoutHandler(customerLoginOutHandler);
        http.csrf().disable();
    }
}
